const app = getApp();
Page({
    data: {
        baseShow: {},//全局显示
        swiperList: [],//轮播图列表
        moldClassIndex: 0,//模考类型索引
        showMask: false,//弹窗显隐
        showEndMask: false,//考试结束弹窗显示
        getParams: {
            page: 0,
            page_size: 20,
            type: 1,//类型 1万人模考(默认值) 2真人模考
        },
        list: [],//题目列表
    },
    async onLoad(options) {
        this.setData({
            baseShow: await wx.$getBase()
        })
    },
    onShow(){
        this.setData({
            'getParams.page':0,
            list:[]
        },()=>{
            this.get_mk_question_list()
        })
    },
    //触底
    scrolltolower(){
      this.get_mk_question_list();
    },
    close() {
        this.setData({
            showMask: false,
            showEndMask:false
        })
    },
    //获取题库
    get_mk_question_list() {
        this.setData({
            'getParams.page': this.data.getParams.page += 1
        })
        wx.$request(wx.$api.mk_question_list, {
            ...this.data.getParams
        }).then(res => {
           console.log(res);


            if (!res.length && this.data.list.length) wx.showToast({
                title:'已全部加载完毕',
                icon:'none'
            })
            res.forEach(item => {
                item.progress = parseInt(item.progress)
                item.examination_s = parseInt(item.examination_s/60)
                if (this.data.getParams.type === 1) {
                    //是否考试结束-结束时间大于当前时间
                    console.log(new Date(item.end_time.replace(/-/g,'/')).getTime(),new Date().getTime())
                    item.is_end = (new Date(item.end_time.replace(/-/g,'/')).getTime() < new Date().getTime());
                    //结束时间
                    let end_time = new Date(item.end_time.replace(/-/g,'/'))
                    item.end_time_str = `${item.start_time.replace(/-/g,'/')}-${String(end_time.getHours()).length === 1 ? '0' + end_time.getHours() : end_time.getHours()}:${String(end_time.getMinutes()).length === 1 ? '0' + end_time.getMinutes() : end_time.getMinutes()}`

                    //考试时长 结束-开始 换分
                    item.time_length = (new Date(item.end_time.replace(/-/g,'/')).getTime() / 1000 - new Date(item.start_time.replace(/-/g,'/')).getTime() / 1000)/60

                }

            })

            let list = [...this.data.list, ...res]
            list.forEach(item=>{
              item.on_line=parseInt(item.on_line/35)
            })
            this.setData({
                list
            })
        })
    },
    //题目点击事件处理
    handlerTap(e) {
        const {index} = e.currentTarget.dataset;
        const item = this.data.list[index];
        if (item.is_use === 2){
            wx.navigateTo({
                url: '/mold-test/pages/unlock/unlock?id='+item.id
            })
        }else {
            if (item.is_end){
                this.setData({
                    showEndMask:true
                })
                return false
            }
            if (!this.data.moldClassIndex){
                if ( new Date(item?.start_time.replace(/-/g,'/')).getTime()>new Date().getTime()){
                    this.setData({
                        showMask:true
                    })
                    return false
                }
            }

            //report_state	string	状态 1未开始(未生成答题卡) 2答题中(已生成答题卡) 3已提交
            /*
            if (item.report_state===1){
                wx.$request(wx.$api.mk_generate_sheet,{
                    question_id:item.id
                }).then(res=>{
                    wx.navigateTo({
                        url:`/mold-test/pages/brushTopic_mk/brushTopic?mid=${item.id}&type=1&rid=${item.report_id}`
                    })
                })
            }
            */
           if (item.report_state===1){
                wx.navigateTo({
                    url:`/mold-test/pages/brushTopic_mk/brushTopic?mid=${item.id}&type=1&rid=${item.report_id}`
                })
            }
            if (item.report_state===2){
                wx.navigateTo({
                    url:`/mold-test/pages/brushTopic_mk/brushTopic?mid=${item.id}&type=2&rid=${item.report_id}`
                })
            }else if (item.report_state===3 && this.data.moldClassIndex){
              /*  wx.$request(wx.$api.mk_generate_sheet,{
                    question_id:item.id
                }).then(res=>{
                    wx.navigateTo({
                        url:`/mold-test/pages/brushTopic_mk/brushTopic?mid=${item.id}&type=2&rid=${item.report_id}`
                    })
                })*/
                wx.navigateTo({
                    url: `/mold-test/pages/examreport_mock/examreport?id=${item.report_id}`
                })
            }else if (item.report_state===3){
                wx.navigateTo({
                    url: `/mold-test/pages/examreport_mock/examreport?id=${item.report_id}`
                })
            }


        }

    },
    //模考类型切换
    switchClass(e) {
        const {index} = e.currentTarget.dataset;
        this.setData({
            moldClassIndex: index,
            'getParams.type': Number(index) + 1,
            list: [],
            'getParams.page': 0
        }, () => {
            this.get_mk_question_list()
        })

    },
    //轮播图跳转
    swiperJump(e) {
        let {swiperList} = this.data;
        let index = e.currentTarget.dataset.index;
        if (swiperList[index].target == "utside") {
            app.globalData.content = swiperList[index].url;
            wx.navigateTo({
                url: '/pages/waparse/waparse?title=' + swiperList[index].title + '&is_url=' + 2,
            })
        } else {
            if (swiperList[index].applets_page_is_tabbar == 1) {
                wx.switchTab({
                    url: swiperList[index].applets_page_path,
                })
            } else if (swiperList[index].target == 'self') {
                if (swiperList[index].parameter) {
                    wx.navigateTo({
                        url: swiperList[index].applets_page_path + '?' + swiperList[index].parameter + '&' + swiperList[index].applets_page_parameter,
                    })
                } else {
                    wx.navigateTo({
                        url: swiperList[index].applets_page_path,
                    })
                }
            } else {
                wx.navigateToMiniProgram({
                    appId: swiperList[index].app_id, // 要跳转的小程序的appid
                    path: swiperList[index].path + '?' + swiperList[index].parameter, // 跳转的目标页面
                    extarData: {
                        open: 'auth'
                    },
                    envVersion: 'release',
                    success(res) {
                        // 打开成功
                    }
                })
            }
        }
    },
    //获取轮播图
    getSwiperList() {
        wx.$request(wx.$api.get_banner).then(res => {
            // console.log(res);
            this.setData({
                swiperList: res
            })
        })
    }
});
